# -*- coding: utf-8 -*- 
import os
import xlsxwriter
import random
from tqdm import tqdm


def randomValue(values:list):
    return values[random.randint(0,len(values)-1)]

def getPeopleName():
    return randomValue(["赵无忌","钱无量","孙无暇","李无忧","周无愧","吴畏惧","郑无惑","王无悔"])
def getOwnerName():
    return randomValue(["程经武","叶瀚漠","田鸿光","刘天禄","罗德辉","王永思","廖乐贤","曹思淼","林承载","毛新翰"])
def getCompanyName():
    return randomValue(["优建运输","极优运输","友飞速运","友聚运输","优统快运"])

def getCityName():
    return "第"+randomValue(["一","二","三","四","五","六","七","八","九"])+"转运中心"

def getCarNo():
    return f"临Z-{random.randint(100000,999999)}"

def getTestDate(isTitles:bool,companyName=None):
    data = []
    companyName = getCompanyName() if companyName is None else companyName
    if isTitles:
        data.append(["物流转运派车单测试数据"])
        data.append(["承运单位：",companyName,None,None,"数据期号：","202111"])
        data.append(["序号","装货单位","卸货单位","承运车号","司机姓名","派车人"])
    else:
        row_count = random.randint(10,100)
        page = 0
        for i in range(1,row_count):
            up = getCityName()
            down = getCityName() if up.startswith("第一") else "第一转运中心"
            data.append([i,up,down,getCarNo(),getOwnerName(),getPeopleName()])

            if all([i%15==0,i!=0]):  
                page += 1              
                data.append(["页码：",page,"交表人：",getOwnerName(),"签收人：",getPeopleName()])
                data = data + getTestDate(True,companyName)
        page += 1              
        data.append(["页码：",page,"交表人：",getOwnerName(),"签收人：",getPeopleName()])
    return data
    
def save_to_new_file(filename,data,sheet_name = "sheet1"):
        '''将数据写入新的xlsx文件（新的xlsx文件路径，二维数据）'''
        # 写入数据
        #print(f"正在将 {len(data)} 条数据，写入新的xlsx文件...")
        workbook = xlsxwriter.Workbook(filename)
        worksheet = workbook.add_worksheet(sheet_name)
        font = workbook.add_format({"font_size":10,"font_family":"Arial"})
        for i in range(len(data)):
            for j in range(len(data[i])):
                worksheet.write(i, j, data[i][j], font)
        # 关闭文件流
        #print("正在写入磁盘，请等待几分钟...")
        workbook.close()
        #print(f"完成，文件写入成功：{filename}")

if __name__ == '__main__':

    output_dir = input("请输入保存测试数据的文件夹(默认：testData\\)：")
    output_dir = output_dir if output_dir!="" else "testData\\"
    output_dir = output_dir if output_dir.endswith("\\") else output_dir+"\\"
    if os.path.isdir(output_dir)==False:
        os.mkdir(output_dir)

    files_count_input = input("请输入生成文件数量（默认：100）：")
    files_count = int(files_count_input) if files_count_input!="" else 100

    print("正在生成测试数据文件...")

    for i in tqdm(range(files_count)):
        row_count = random.randint(10,100)
        companyName = getCompanyName()
        data = getTestDate(True,companyName)
        data = data + getTestDate(False,companyName)
        filename = f"{output_dir}{companyName}{i}.xls"
        save_to_new_file(filename,data)
    
    print("完成")

